package com.qianya.weishi_server.config.logback;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.CompositeConverter;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.Ansi.Color;

import static org.fusesource.jansi.Ansi.ansi;

/**
 * @author LLL
 */
public class ColorByLevel extends CompositeConverter<ILoggingEvent> {

    @Override
    protected String transform(ILoggingEvent event, String in) {
        Ansi.setEnabled(true);
        Level level = event.getLevel();
        switch (level.toInt()) {
            case Level.ERROR_INT: {
                return ansi().fgBright(Color.RED).a(in).reset().toString();
            }
            case Level.WARN_INT: {
                return ansi().fg(Color.RED).a(in).reset().toString();
            }
            case Level.INFO_INT: {
                return ansi().fg(Color.GREEN).a(in).reset().toString();
            }
            case Level.DEBUG_INT: {
                return ansi().fgBright(Color.YELLOW).a(in).reset().toString();
            }
            default: {
                return in;
            }
        }
    }

}
